package com.example.sharesdk;

import java.util.HashMap;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.utils.Misc;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity implements PlatformActionListener,Callback {

    private String text = "@微兼职APP: 这是我的分享测试数据！~我只是来酱油的！~请不要在意 好不好？？？？？";
    private String imageurl = "http://h.hiphotos.baidu.com/image/pic/item/ac4bd11373f082028dc9b2a749fbfbedaa641bca.jpg";
    private String title = "微兼职";
    private String url = "www.baidu.com";

    //public static final String SHARE_APP_KEY = "21b0f35691b8";
    private Button shareButton;

    private SharePopupWindow share;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_share);
        ShareSDK.initSDK(this);
        shareButton = (Button) findViewById(R.id.share);

        shareButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                share = new SharePopupWindow(MainActivity.this);
                share.setPlatformActionListener(MainActivity.this);
                ShareModel model = new ShareModel();
                model.setImageUrl(imageurl);
                model.setText(text);
                model.setTitle(title);
                model.setUrl(url);
                share.initShareParams(model);
                share.showShareWindow();
                // 显示窗口 (设置layout在PopupWindow中显示的位置)
                share.showAtLocation(MainActivity.this.findViewById(R.id.main),
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (share != null) {
            share.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(this);
    }

    @Override
    public void onCancel(Platform arg0, int arg1) {
        Message msg = new Message();
        msg.what = 0;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onComplete(Platform plat, int action,
            HashMap<String, Object> res) {
        Message msg = new Message();
        msg.arg1 = 1;
        msg.arg2 = action;
        msg.obj = plat;
        Misc.alert("分享成功了。。。。。");
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public void onError(Platform arg0, int arg1, Throwable arg2) {
        Message msg = new Message();
        msg.what = 1;
        UIHandler.sendMessage(msg, this);
    }

    @Override
    public boolean handleMessage(Message msg) {
        int what = msg.what;
        if (what == 1) {
            Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
        }
        if (share != null) {
            share.dismiss();
        }
        return false;
    }
}
